<?php
require_once APPLICATION_PATH.'/models/Cate.php';
require_once APPLICATION_PATH.'/models/Slide.php';
require_once APPLICATION_PATH.'/models/Article.php';
require_once APPLICATION_PATH.'/models/Plate.php';

require_once 'BaseController.php';

class IndexController extends BaseController
{

    public function indexAction()
    {
        //加载首页幻灯片
        $tblslide = new Slide();
        $this->view->slide = $tblslide->getSlide();
        //加载板块
        $tblplates = new Plate();
        $this->view->plates = $tblplates->getPlates();

        $this->view->tblCate = new Cate();
        $this->view->tblArticle = new Article();
    }

    public function aclistAction(){
        $cateid = $this->_getParam('cateid');
        $search = $this->_getParam('search');
        $tblart = new Article();
        $arts = null;
        if(!is_numeric($cateid)){
            if($search != null){
                $arts = $tblart->getArticlesByKeyword($search);
                $this->view->catename = '搜索：'.$search;
            }else{
                $this->paramerror();
                return;
            }
        }
        else{
            $arts = $tblart->getArticlesByCate($cateid);
            $tblcate = new Cate();
            $this->view->catename = $tblcate->getCateName($cateid);
        }

        $this->view->articles = $arts;
        $rmdArts = $tblart->getRecommendArticles();
        $this->view->recommendArts = $rmdArts;
    }

    public function detailAction(){
        $aid = $this->_getParam('id');

        if(!is_numeric($aid)){
            $this->paramerror();
            return;
        }

        $tblart = new Article();
        $art = $tblart->getArticleById($aid);
        if($art == null){
            $this->paramerror();
            return;
        }
        $art = $art->toArray();

        //更新访问数
        $set = array (
            'viewnum' => ++$art['viewnum'],
        );
        $where = ("id=".$art['id']);
        $tblart->update($set, $where);

        $tblcate = new Cate();
        $art['catename'] = $tblcate->getCateName($art['cateid']);
        $art['pre'] = $tblart->getPreArticle($art['posttime'], $art['cateid']);
        $art['next'] = $tblart->getNextArticle($art['posttime'], $art['cateid']);

        $this->view->article = $art;

        $rmdArts = $tblart->getRecommendArticles();
        $this->view->recommendArts = $rmdArts;
    }

    public function paramerror(){
        echo '<script>alert("没有您要找的文章！");window.location.href="'.INDEX_PATH.'"; </script>';
        $this->render('none');
    }
}
